﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Chengqbd.Core.Extensions
{
	public static class StringExtensions
	{
		public static string Remove(this string value, params char[] removeChar)
		{
			bool flag = !string.IsNullOrEmpty(value) && removeChar != null;
			if (flag)
			{
				Array.ForEach<char>(removeChar, delegate(char c)
				{
					value = value.Remove(c.ToString(), RegexOptions.None);
				});
			}
			return value;
		}
        
		public static string Remove(this string value, params string[] strings)
		{
			return strings.Aggregate(value, (string current, string c) => current.Replace(c, string.Empty));
		}
        
		public static string Remove(this string value, int index, bool isLeft = true)
		{
			bool flag = value.Length <= index;
			string result;
			if (flag)
			{
				result = "";
			}
			else if (isLeft)
			{
				result = value.Substring(index);
			}
			else
			{
				result = value.Substring(0, value.Length - index);
			}
			return result;
		}
        
		public static string RemoveAllSpecialCharacters(this string value)
		{
			StringBuilder stringBuilder = new StringBuilder(value.Length);
			foreach (char value2 in value.Where(new Func<char, bool>(char.IsLetterOrDigit)))
			{
				stringBuilder.Append(value2);
			}
			return stringBuilder.ToString();
		}
        
		public static string Remove(this string str, string tag, RegexOptions options = RegexOptions.None)
		{
			bool flag = string.IsNullOrWhiteSpace(str);
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				result = (tag.IsNullOrEmpty() ? str : Regex.Replace(str, tag, "", options));
			}
			return result;
		}
        
		public static string FormatWith(this string format, params object[] args)
		{
			return string.Format(CultureInfo.CurrentCulture, format, args);
		}
        
		public static string ReverseString(this string value)
		{
			return new string(value.Reverse<char>().ToArray<char>());
		}
        
		public static string[] Split(this string value, string strSplit, bool removeEmptyEntries = false)
		{
			return value.Split(new string[]
			{
				strSplit
			}, removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
		}
        
		public static int GetTextLength(this string value)
		{
			ASCIIEncoding asciiencoding = new ASCIIEncoding();
			int num = 0;
			byte[] bytes = asciiencoding.GetBytes(value);
			foreach (byte b in bytes)
			{
				bool flag = b == 63;
				if (flag)
				{
					num += 2;
				}
				else
				{
					num++;
				}
			}
			return num;
		}
        
		public static string TrimToMaxLength(this string value, int maxLength)
		{
			return (value == null || value.Length <= maxLength) ? value : value.Substring(0, maxLength);
		}
        
		public static string TrimToMaxLength(this string value, int maxLength, string suffix)
		{
			return (value == null || value.Length <= maxLength) ? value : (value.Substring(0, maxLength) + suffix);
		}
        
		public static string Truncate(this string value, int length, bool userElipse = false)
		{
			int num = userElipse ? 3 : 0;
			bool flag = length - num <= 0;
			if (flag)
			{
				throw new InvalidOperationException(string.Format("Length must be greater than {0}.", num));
			}
			bool flag2 = value.IsNullOrEmpty() || value.Length <= length;
			string result;
			if (flag2)
			{
				result = value;
			}
			else
			{
				result = value.Substring(0, length - num) + new string('.', num);
			}
			return result;
		}
        
		public static string PadBoth(this string value, int width, char padChar, bool truncate = false)
		{
			int num = width - value.Length;
			bool flag = num == 0 || (num < 0 && !truncate);
			string result;
			if (flag)
			{
				result = value;
			}
			else
			{
				bool flag2 = num < 0;
				if (flag2)
				{
					result = value.Substring(0, width);
				}
				else
				{
					result = value.PadLeft(width - num / 2, padChar).PadRight(width, padChar);
				}
			}
			return result;
		}
        
		public static string EnsureStartsWith(this string value, string prefix)
		{
			return value.StartsWith(prefix) ? value : (prefix + value);
		}
        
		public static string EnsureEndWith(this string value, string suffix)
		{
			return value.EndsWith(suffix) ? value : (value + suffix);
		}
        
		public static string Repeat(this string value, int repeatCount)
		{
			bool flag = value.Length == 1;
			string result;
			if (flag)
			{
				result = new string(value[0], repeatCount);
			}
			else
			{
				StringBuilder stringBuilder = new StringBuilder(repeatCount * value.Length);
				while (repeatCount-- > 0)
				{
					stringBuilder.Append(value);
				}
				result = stringBuilder.ToString();
			}
			return result;
		}
        
		public static string ExtractNumber(this string value)
		{
			return value.Where(new Func<char, bool>(char.IsDigit)).Aggregate(new StringBuilder(value.Length), (StringBuilder sb, char c) => sb.Append(c)).ToString();
		}
        
		public static string ConcatWith(this string value, params string[] values)
		{
			return value + string.Concat(values);
		}
        
		public static string Join<T>(this string value, string separator, T[] obj)
		{
			bool flag = obj == null || obj.Length == 0;
			string result;
			if (flag)
			{
				result = value;
			}
			else
			{
				bool flag2 = separator == null;
				if (flag2)
				{
					separator = string.Empty;
				}
				Converter<T, string> converter = (T o) => o.ToString();
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append(value);
				stringBuilder.Append(separator);
				stringBuilder.Append(string.Join(separator, Array.ConvertAll<T, string>(obj, converter)));
				result = stringBuilder.ToString();
			}
			return result;
		}
        
		public static string JoinNotNullOrEmpty(this string[] values, string separator)
		{
			List<string> list = (from s in values
			where !string.IsNullOrEmpty(s)
			select s).ToList<string>();
			return string.Join(separator, list.ToArray());
		}
        
		public static string GetBefore(this string value, string x)
		{
			int num = value.IndexOf(x, StringComparison.Ordinal);
			return (num == -1) ? string.Empty : value.Substring(0, num);
		}
        
		public static string GetBetween(this string value, string x, string y)
		{
			int num = value.IndexOf(x, StringComparison.Ordinal);
			int num2 = value.LastIndexOf(y, StringComparison.Ordinal);
			bool flag = num == -1 || num2 == -1;
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				int num3 = num + x.Length;
				result = ((num3 >= num2) ? string.Empty : value.Substring(num3, num2 - num3).Trim());
			}
			return result;
		}
        
		public static string GetAfter(this string value, string x)
		{
			int num = value.IndexOf(x, StringComparison.Ordinal);
			bool flag = num == -1;
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				int num2 = num + x.Length;
				result = ((num2 >= value.Length) ? string.Empty : value.Substring(num2).Trim());
			}
			return result;
		}
        
		public static string SubstringFrom(this string value, int index)
		{
			return (index < 0 && index < value.Length) ? value : value.Substring(index, value.Length - index);
		}
        
		public static string ToUpperFirstLetter(this string value)
		{
			return StringExtensions.ToFirstLetter(value, true);
		}
        
		public static string ToLowerFirstLetter(this string value)
		{
			return StringExtensions.ToFirstLetter(value, false);
		}
        
		private static string ToFirstLetter(string value, bool isUpper = true)
		{
			bool flag = value.IsNullOrEmpty();
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				char[] array = value.ToCharArray();
				if (isUpper)
				{
					array[0] = char.ToUpper(array[0]);
				}
				else
				{
					array[0] = char.ToLower(array[0]);
				}
				result = new string(array);
			}
			return result;
		}
        
		public static string ToTitleCase(this string value)
		{
			return value.ToTitleCase(CultureInfo.CurrentCulture);
		}
        
		public static string ToTitleCase(this string value, CultureInfo culture)
		{
			return culture.TextInfo.ToTitleCase(value);
		}
        
		public static string ToPlural(this string singular)
		{
			int num = singular.LastIndexOf(" of ", StringComparison.Ordinal);
			bool flag = num > 0;
			string result;
			if (flag)
			{
				result = singular.Substring(0, num) + singular.Remove(0, num).ToPlural();
			}
			else
			{
				bool flag2 = singular.EndsWith("sh") || singular.EndsWith("ch") || singular.EndsWith("us") || singular.EndsWith("ss");
				if (flag2)
				{
					result = singular + "es";
				}
				else
				{
					bool flag3 = singular.EndsWith("y");
					if (flag3)
					{
						result = singular.Remove(singular.Length - 1, 1) + "ies";
					}
					else
					{
						bool flag4 = singular.EndsWith("o");
						if (flag4)
						{
							result = singular.Remove(singular.Length - 1, 1) + "oes";
						}
						else
						{
							result = singular + "s";
						}
					}
				}
			}
			return result;
		}
        
		public static string ReplaceAll(this string value, IEnumerable<string> oldValues, Func<string, string> replacePredicate)
		{
			StringBuilder stringBuilder = new StringBuilder(value);
			foreach (string text in oldValues)
			{
				string newValue = replacePredicate(text);
				stringBuilder.Replace(text, newValue);
			}
			return stringBuilder.ToString();
		}
        
		public static string ReplaceAll(this string value, IEnumerable<string> oldValues, string newValue)
		{
			StringBuilder stringBuilder = new StringBuilder(value);
			foreach (string oldValue in oldValues)
			{
				stringBuilder.Replace(oldValue, newValue);
			}
			return stringBuilder.ToString();
		}
        
		public static string ReplaceAll(this string value, IEnumerable<string> oldValues, IEnumerable<string> newValues)
		{
			StringBuilder stringBuilder = new StringBuilder(value);
			IEnumerator<string> enumerator = newValues.GetEnumerator();
			foreach (string oldValue in oldValues)
			{
				bool flag = !enumerator.MoveNext();
				if (flag)
				{
					throw new ArgumentOutOfRangeException("newValues", "newValues sequence is shorter than oldValues sequence");
				}
				stringBuilder.Replace(oldValue, enumerator.Current);
			}
			bool flag2 = enumerator.MoveNext();
			if (flag2)
			{
				throw new ArgumentOutOfRangeException("newValues", "newValues sequence is longer than oldValues sequence");
			}
			return stringBuilder.ToString();
		}
        
		public static string EncodeEmailAddress(this string emailAddress)
		{
			string text = emailAddress;
			int i = text.Length;
			while (i >= 1)
			{
				int num = Convert.ToInt32(text[i - 1]);
				int num2 = num;
				int num3 = num2;
				string text2;
				if (num3 <= 34)
				{
					if (num3 != 32)
					{
						if (num3 != 34)
						{
							goto IL_7C;
						}
						text2 = "\"";
					}
					else
					{
						text2 = " ";
					}
				}
				else if (num3 != 38)
				{
					if (num3 != 60)
					{
						if (num3 != 62)
						{
							goto IL_7C;
						}
						text2 = ">";
					}
					else
					{
						text2 = "<";
					}
				}
				else
				{
					text2 = "&";
				}
				IL_C6:
				bool flag = text2.Length > 0;
				if (flag)
				{
					text = text.Substring(0, i - 1) + text2 + text.Substring(i);
				}
				i--;
				continue;
				IL_7C:
				bool flag2 = num >= 32 && num <= 127;
				if (flag2)
				{
					text2 = "&#" + Convert.ToString(num) + ";";
				}
				else
				{
					text2 = "&#" + Convert.ToString(num) + ";";
				}
				goto IL_C6;
			}
			return text;
		}
        
		public static string RepairZero(this string text, int limitedLength)
		{
			return text.PadLeft(limitedLength, '0');
		}
        
		public static string ReplaceFirst(this string @this, string oldValue, string newValue)
		{
			int num = @this.IndexOf(oldValue, StringComparison.Ordinal);
			bool flag = num == -1;
			string result;
			if (flag)
			{
				result = @this;
			}
			else
			{
				result = @this.Remove(num, oldValue.Length).Insert(num, newValue);
			}
			return result;
		}
        
		public static string ReplaceFirst(this string @this, int number, string oldValue, string newValue)
		{
			List<string> source = @this.Split(oldValue, false).ToList<string>();
			int count = number + 1;
			IEnumerable<string> values = source.Take(count);
			IEnumerable<string> enumerable = source.Skip(count);
			return string.Join(newValue, values) + (enumerable.Any<string>() ? oldValue : "") + string.Join(oldValue, enumerable);
		}
        
		public static string ReplaceLast(this string @this, string oldValue, string newValue)
		{
			int num = @this.LastIndexOf(oldValue, StringComparison.Ordinal);
			bool flag = num == -1;
			string result;
			if (flag)
			{
				result = @this;
			}
			else
			{
				result = @this.Remove(num, oldValue.Length).Insert(num, newValue);
			}
			return result;
		}
        
		public static string ReplaceLast(this string @this, int number, string oldValue, string newValue)
		{
			List<string> list = @this.Split(oldValue, false).ToList<string>();
			int num = Math.Max(0, list.Count - number - 1);
			IEnumerable<string> values = list.Take(num);
			IEnumerable<string> values2 = list.Skip(num);
			return string.Join(oldValue, values) + ((num > 0) ? oldValue : "") + string.Join(newValue, values2);
		}
        
		public static string Left(this string value, int length)
		{
			bool flag = value == null;
			if (flag)
			{
				throw new ArgumentNullException("value");
			}
			bool flag2 = length >= value.Length;
			if (flag2)
			{
				throw new ArgumentOutOfRangeException("length", length, "length 不能大于给定字符串的长度");
			}
			return value.Substring(0, length);
		}
        
		public static string Right(this string value, int length)
		{
			bool flag = value == null;
			if (flag)
			{
				throw new ArgumentNullException("value");
			}
			bool flag2 = length >= value.Length;
			if (flag2)
			{
				throw new ArgumentOutOfRangeException("length", length, "length 不能大于给定字符串的长度");
			}
			return value.Substring(value.Length - length, length);
		}
	}
}
